package com.zxiw.zxedu.recyclerview.group

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.annotation.IntDef
import com.zxiw.zxedu.recyclerview.adapter.HelperRecyclerViewHolder


/**
 * @author zhangxiaoming
 * @date 2023/8/18
 * @description
 * <p>描述：提供分组便捷操作,可设置状态的错误页面、空页面、加载中页面、内容页面自由切换
 */
@SuppressLint("NotifyDataSetChanged")
abstract class GroupedStateRecyclerViewAdapter<T>(context: Context) :
    GroupedRecyclerViewAdapter<T>(context) {
    @State
    open var state = STATE_NORMAL
        set(@State state) {
            field = state
            if (xRecyclerView != null) {
                when (state) {
                    STATE_LOADING, STATE_EMPTY, STATE_ERROR ->
                        xRecyclerView!!.setEnabledScroll(false)
                    STATE_NORMAL -> xRecyclerView!!.setEnabledScroll(true)
                }
            }
            notifyDataSetChanged()
        }


    @IntDef(STATE_NORMAL, STATE_LOADING, STATE_EMPTY, STATE_ERROR)
    @Retention(AnnotationRetention.SOURCE)
    annotation class State
    companion object {
        const val STATE_NORMAL = 0
        const val STATE_LOADING = 1
        const val STATE_EMPTY = 2
        const val STATE_ERROR = 3

        const val TYPE_LOADING = 1000
        const val TYPE_EMPTY = 1001
        const val TYPE_ERROR = 1002
    }

    override fun getItemCount(): Int {
        return when (state) {
            STATE_LOADING,
            STATE_EMPTY,
            STATE_ERROR -> 1
            else -> super.getItemCount()
        }
    }

    override fun getItemViewType(position: Int): Int {
        return when (state) {
            STATE_LOADING -> TYPE_LOADING
            STATE_EMPTY -> TYPE_EMPTY
            STATE_ERROR -> TYPE_ERROR
            else -> itemViewType(position)
        }
    }

    protected fun itemViewType(position: Int): Int {
        return super.getItemViewType(position)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HelperRecyclerViewHolder {
        return when (viewType) {
            TYPE_LOADING -> HelperRecyclerViewHolder(getLoadingView(parent), 0)
            TYPE_EMPTY -> HelperRecyclerViewHolder(getEmptyView(parent), 0)
            TYPE_ERROR -> HelperRecyclerViewHolder(getErrorView(parent), 0)
            else -> super.onCreateViewHolder(parent, viewType)
        }
    }

    override fun onBindViewHolder(holder: HelperRecyclerViewHolder, position: Int) {
        when (state) {
            STATE_LOADING -> onBindLoadingViewHolder(holder)
            STATE_EMPTY -> onBindEmptyViewHolder(holder)
            STATE_ERROR -> onBindErrorViewHolder(holder)
            else -> viewHolder(holder, position)
        }
    }

    open fun viewHolder(viewHolder: HelperRecyclerViewHolder?, position: Int) {
        super.onBindViewHolder(viewHolder!!, position)
    }

    open fun onBindErrorViewHolder(holder: HelperRecyclerViewHolder) {}

    open fun onBindEmptyViewHolder(holder: HelperRecyclerViewHolder) {}

    open fun onBindLoadingViewHolder(holder: HelperRecyclerViewHolder) {}
    abstract fun getEmptyView(parent: ViewGroup): View

    abstract fun getErrorView(parent: ViewGroup): View

    abstract fun getLoadingView(parent: ViewGroup): View

    private fun invalidateState() {
        if (super.getItemCount() > 0) {
            state = STATE_NORMAL
        } else {
            state = STATE_EMPTY
        }
    }

    private fun count(): Int {
        return countGroupRangeItem(0, mStructures.size)
    }

    override fun setGroups(datas: List<T>): Boolean {
        val result = super.setGroups(datas)
        invalidateState()
        return result
    }

    override fun removeAll() {
        super.removeAll()
        invalidateState()
    }

    override fun removeGroup(groupPosition: Int) {
        super.removeGroup(groupPosition)
        invalidateState()
    }

    override fun removeRangeGroup(groupPosition: Int, count: Int) {
        super.removeRangeGroup(groupPosition, count)
        invalidateState()
    }

    override fun insertGroup(groupPosition: Int) {
        super.insertGroup(groupPosition)
        invalidateState()
    }

    override fun insertRangeGroup(groupPosition: Int, count: Int) {
        super.insertRangeGroup(groupPosition, count)
        invalidateState()
    }

    override fun insertHeader(groupPosition: Int) {
        super.insertHeader(groupPosition)
        invalidateState()
    }

    override fun insertFooter(groupPosition: Int) {
        super.insertFooter(groupPosition)
        invalidateState()
    }

    override fun insertRangeChild(groupPosition: Int, childPosition: Int, count: Int) {
        super.insertRangeChild(groupPosition, childPosition, count)
        invalidateState()
    }
}